home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Libraries / BSP Tree 1.2 / Sources / Graphics / source / transform_3d.cp < prev    next >
Encoding:
Text File  |  1995-03-26  |  8.4 KB  |  149 lines  |  [TEXT/MMCC]

  1. //------------------------------------------------------------------------------
  2. //    File:                    transform.cp
  3. //    Date:                    9/4/94
  4. //    Author:                Bretton Wade
  5. //
  6. //    Description:    this file contains methods for the transformations matrices
  7. //
  8. //------------------------------------------------------------------------------
  9.  
  10. #include "utility.h"
  11. #include "transform_3d.h"
  12.  
  13. //------------------------------------------------------------------------------
  14. //    Transformation to rotate a point_3d about the x axis
  15. //------------------------------------------------------------------------------
  16. matrix_3d    RotateX (real angle)                                                                                                    //    build a transformation matrix_3d
  17. {                                                                                                                                                                //    begin
  18.     angle = DegreesToRadians (angle);                                                                                            //    convert degrees to radians
  19.     real    cosine = COS (angle),                                                                                                        //    compute the cosine of the angle
  20.                 sine = SIN (angle);                                                                                                            //    compute the sine of the angle
  21.     matrix_3d    m (matrix_3d::identity);                                                                                        //    new identity matrix_3d
  22.     m (1, 1) = cosine;                                                                                                                        //    scale the y rotation by the cosine
  23.     m (2, 2) = cosine;                                                                                                                        //    scale the z rotation by the cosine
  24.     m (2, 1) = -sine;                                                                                                                            //    scale the y rotation by the -sine
  25.     m (1, 2) = sine;                                                                                                                            //    scale the z rotation by the sine
  26.     return m;                                                                                                                                            //    return the matrix_3d
  27. }                                                                                                                                                                //    end
  28.  
  29. //------------------------------------------------------------------------------
  30. //    Transformation to rotate a point_3d about the y axis
  31. //------------------------------------------------------------------------------
  32. matrix_3d    RotateY (real angle)                                                                                                    //    build a transformation matrix_3d
  33. {                                                                                                                                                                //    begin
  34.     angle = DegreesToRadians (angle);                                                                                            //    convert degrees to radians
  35.     real    cosine = COS (angle),                                                                                                        //    compute the cosine of the angle
  36.                 sine = SIN (angle);                                                                                                            //    compute the sine of the angle
  37.     matrix_3d    m (matrix_3d::identity);                                                                                        //    new identity matrix_3d
  38.     m (0, 0) = cosine;                                                                                                                        //    scale the x rotation by the cosine
  39.     m (2, 2) = cosine;                                                                                                                        //    scale the z rotation by the cosine
  40.     m (2, 0) = sine;                                                                                                                            //    scale the x rotation by the sine
  41.     m (0, 2) = -sine;                                                                                                                            //    scale the z rotation by the -sine
  42.     return m;                                                                                                                                            //    return the matrix_3d
  43. }                                                                                                                                                                //    end
  44.  
  45. //------------------------------------------------------------------------------
  46. //    Transformation to rotate a point_3d about the z axis
  47. //------------------------------------------------------------------------------
  48. matrix_3d    RotateZ (real angle)                                                                                                    //    build a transformation matrix_3d
  49. {                                                                                                                                                                //    begin
  50.     angle = DegreesToRadians (angle);                                                                                            //    convert degrees to radians
  51.     real    cosine = COS (angle),                                                                                                        //    compute the cosine of the angle
  52.             sine = SIN (angle);                                                                                                                //    compute the sine of the angle
  53.     matrix_3d    m (matrix_3d::identity);                                                                                        //    new identity matrix_3d
  54.     m (0, 0) = cosine;                                                                                                                        //    scale the x rotation by the cosine
  55.     m (1, 1) = cosine;                                                                                                                        //    scale the y rotation by the cosine
  56.     m (1, 0) = -sine;                                                                                                                            //    scale the x rotation by the -sine
  57.     m (0, 1) = sine;                                                                                                                            //    scale the y rotation by the sine
  58.     return m;                                                                                                                                            //    return the matrix_3d
  59. }                                                                                                                                                                //    end
  60.  
  61. //------------------------------------------------------------------------------
  62. //    Transformation to translate a point_3d by a specified amount
  63. //------------------------------------------------------------------------------
  64. matrix_3d    Translate (real x, real y, real z)                                                                        //    build a transformation matrix_3d
  65. {                                                                                                                                                                //    begin
  66.     matrix_3d    m (matrix_3d::identity);                                                                                        //    new identity matrix_3d
  67.     m (3, 0) = x;                                                                                                                                    //    translate the x axis
  68.     m (3, 1) = y;                                                                                                                                    //    translate the y axis
  69.     m (3, 2) = z;                                                                                                                                    //    translate the z axis
  70.     return m;                                                                                                                                            //    return the matrix_3d
  71. }                                                                                                                                                                //    end
  72.  
  73. //------------------------------------------------------------------------------
  74. //    Transformation to scale a point_3d by a specified amount
  75. //------------------------------------------------------------------------------
  76. matrix_3d    Scale (real x, real y, real z)                                                                                //    build a transformation matrix_3d
  77. {                                                                                                                                                                //    begin
  78.     matrix_3d    m (matrix_3d::identity);                                                                                        //    new identity matrix_3d
  79.     m (0, 0) = x;                                                                                                                                    //    scale the x axis
  80.     m (1, 1) = y;                                                                                                                                    //    scale the y axis
  81.     m (2, 2) = z;                                                                                                                                    //    scale the z axis
  82.     return m;                                                                                                                                            //    return the matrix_3d
  83. }                                                                                                                                                                //    end
  84.  
  85. //------------------------------------------------------------------------------
  86. //    Transformation to produce a perspective projection
  87. //------------------------------------------------------------------------------
  88. matrix_3d    Perspective (real distance)                                                                                        //    build a transformation matrix_3d
  89. {                                                                                                                                                                //    begin
  90.     matrix_3d    m (matrix_3d::identity);                                                                                        //    new identity matrix_3d
  91.     m (2, 3) = -1.0 / distance;                                                                                                        //    set the perspective factor to 1.0 / distance to the picture plane_3d
  92.     return m;                                                                                                                                            //    return the matrix_3d
  93. }                                                                                                                                                                //    end
  94.  
  95. //------------------------------------------------------------------------------
  96. //    Transformation for rotating to an arbitrary normalized basis
  97. //------------------------------------------------------------------------------
  98. matrix_3d    VectorMatrix (tuple_3d &n)                                                                                        //    build a vector_3d rotation matrix_3d
  99. {                                                                                                                                                                //    begin
  100.     vector_3d    t (n),                                                                                                                            //    copy it
  101.                         w (n);
  102.     t[w.MinorAxis ()] = 1.0;                                                                                                            //    set the minor axis to 1.0
  103.     vector_3d    u = (t ^ w).Normalize (),                                                                                        //    compute a normal perpendicular vector_3d to w and t
  104.                     v = w ^ u;                                                                                                                        //    compute a normal perpendicular vector_3d to w and u
  105.     matrix_3d    m (matrix_3d::identity);                                                                                        //    new identity matrix_3d
  106.     m (0, 0) = u[X];                                                                                                                            //    assign the first row
  107.     m (0, 1) = u[Y];
  108.     m (0, 2) = u[Z];
  109.     
  110.     m (1, 0) = v[X];                                                                                                                            //    assign the second row
  111.     m (1, 1) = v[Y];
  112.     m (1, 2) = v[Z];
  113.     
  114.     m (2, 0) = w[X];                                                                                                                            //    assign the third row
  115.     m (2, 1) = w[Y];
  116.     m (2, 2) = w[Z];
  117.     return m;                                                                                                                                            //    return the matrix_3d
  118. }                                                                                                                                                                //    end
  119.  
  120. //------------------------------------------------------------------------------
  121. //    Transformation for rotating to an arbitrary basis at an arbitrary location
  122. //------------------------------------------------------------------------------
  123. matrix_3d    ViewMatrix (const vector_3d &u, const vector_3d &v, const vector_3d &n, const point_3d &r)    //    build a viewing matrix_3d from a vector_3d set
  124. {                                                                                                                                                                //    begin
  125.     matrix_3d    m;                                                                                                                                    //    new matrix_3d
  126.     m (0, 0) = u[X];                                                                                                                            //    assign the first column
  127.     m (1, 0) = u[Y];
  128.     m (2, 0) = u[Z];
  129.     m (3, 0) = -(r | u);
  130.     
  131.     m (0, 1) = v[X];                                                                                                                            //    assign the second column
  132.     m (1, 1) = v[Y];
  133.     m (2, 1) = v[Z];
  134.     m (3, 1) = -(r | v);
  135.     
  136.     m (0, 2) = n[X];                                                                                                                            //    assign the third column
  137.     m (1, 2) = n[Y];
  138.     m (2, 2) = n[Z];
  139.     m (3, 2) = -(r | n);
  140.     
  141.     m (0, 3) = R(0.0);                                                                                                                        //    assign the fourth column
  142.     m (1, 3) = R(0.0);
  143.     m (2, 3) = R(0.0);
  144.     m (3, 3) = R(1.0);
  145.     return m;                                                                                                                                            //    return the matrix_3d
  146. }                                                                                                                                                                //    end
  147.  
  148. //------------------------------------------------------------------------------
  149.